package dao.mapper;

import bean.User;
import dao.UserMapper;
import util.DBUtil;
import util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
public class UserMapperImpl implements UserMapper {

    @Override
    public int addUser(String phone_number, String password) {
        if(StringUtil.isEmpty(phone_number)||StringUtil.isEmpty(password)){
            return -1; //参数有误
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql="insert into users (phone_number,password) values (?,?);";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,phone_number);
            preparedStatement.setString(2,password);
            //3，执行sql语句
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return 0;
    }

    @Override
    public int login(String phone_number, String password) {
        if(StringUtil.isEmpty(phone_number)||StringUtil.isEmpty(password)){
            return -1; //参数有误
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql="select password from users where `phone_number` = ?;";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,phone_number);
            //3，执行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            String selected_password = null;
            if(resultSet.next()){
                selected_password = resultSet.getString("password");
            }
            if(!StringUtil.isEmpty(selected_password) && selected_password.equals(password)) return 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return 0;

    }

    @Override
    public User getUserByPhoneNumber(String phoneNumber) {
        if (StringUtil.isEmpty(phoneNumber)){
            return null ;//参数有误
        }
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql="select * from users where `phone_number` = ?;";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,phoneNumber);
            //3，执行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //根据查询结果构造user对象
            User user=null;
            if (resultSet.next()) {
                // 从 resultSet 中获取每一列的数据
                int id = resultSet.getInt("id");
                String phone_number = resultSet.getString("phone_number");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                int gender = resultSet.getInt("gender");
                double account_balance = resultSet.getDouble("account_balance");
                String avatar_url = resultSet.getString("avatar_url");
                //创建对象
                user = new User();
                user.setUid(id);
                user.setPhoneNumber(phoneNumber);
                user.setNickname(username);
                user.setSex(gender);
                user.setBalanceOfAccount(account_balance);
                user.setAvatarAddress(avatar_url);
            }
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return null;
    }


    /**
     * 修改密码
     * @param uid
     * @param newPassword
     */
    @Override
    public int changePassword(int uid, String newPassword) {
        if(uid<=0||StringUtil.isEmpty(newPassword)){
            return -1;// 参数有误
        }

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql="UPDATE users SET password = ? WHERE id = ?;";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,newPassword);
            preparedStatement.setInt(2,uid);
            //3，执行sql语句
            int ret = preparedStatement.executeUpdate();
            System.out.println("ret" + ret);
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return -1;

    }

    public int upDataByUid(int uid, String phoneNumber, String username, String gender) {
        if(uid<=0){
            return -1;//参数有误
        }

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection= DBUtil.getConnection();
            //2，构造失去了语句
            String sql = "UPDATE users SET phone_number = ?, username = ?, gender = ? WHERE id = ?";

            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, phoneNumber);
            preparedStatement.setString(2, username);
            preparedStatement.setString(3, gender);
            preparedStatement.setInt(4, uid);
            int ret = preparedStatement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
        return -1;

    }
}
